package com.example.kafka_demo.service.impl;

import com.example.kafka_demo.dto.ParamsDetail;
import com.example.kafka_demo.service.KafkaService;
import com.example.kafka_demo.util.JsonUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * @author Keson
 * @version 1.0
 * @description: TODO KafkaService实现类
 * @date 2021/9/6 15:01
 */
@Service
@Slf4j
public class KafkaServiceImpl implements KafkaService {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Value("${kafka.topic.kafka-demo-topic}")
    private String topic;

    @Value("${kafka.topic.kafka-demo-key}")
    private String key;

    @Override
    public void sendMessage(ParamsDetail detail) {
        try {
            kafkaTemplate.send(topic, key, JsonUtil.toJson(detail));
        } catch (JsonProcessingException e){
            log.error("对象转换为Json出错！");
        }
    }
}
